package com.winit.openapi.tms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.tms.spi.transportationdoc.TransportationDocService;
import com.winit.tms.spi.transportationdoc.command.TransportationDocOrderPageQueryCommand;
import com.winit.tms.spi.transportationdoc.command.TransportationDocQueryCommand;
import com.winit.tms.spi.transportationdoc.vo.TransportationDocOrderStatusVo;
import com.winit.tms.spi.transportationdoc.vo.TransportationDocVo;
import com.winit.ubarrier.common.util.JSON;

/**
 * 预约单关联单据操作 Controller
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * ming.chen 	1.0  		2017-9-8 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/tms/transportDoc")
public class TransportationDocumentController extends BaseController {
	@Resource
	private TransportationDocService transportationDocService;

    /**
     * 按orderNo查询运输资料列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "getDocsByWinitOrderNo")
    public String getDocsByWinitOrderNo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        TransportationDocQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new TransportationDocQueryCommand();
            command.setOrderNo(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<TransportationDocVo> transportationDocs = transportationDocService.queryDocsByOrderNo(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        data.put("transportationDocs", transportationDocs);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 按winitOrderNo查询提单资料列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "getOrderList")
    public String getOrderList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        TransportationDocOrderPageQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new TransportationDocOrderPageQueryCommand();
            if(json.containsKey("orderNo") && !StringUtils.isEmpty(json.getString("orderNo"))){
                command.setOrderNo(json.getString("orderNo"));
            }
            if(json.containsKey("importDeclarationType") && !StringUtils.isEmpty(json.getString("importDeclarationType"))){
                command.setImportDeclarationType(json.getString("importDeclarationType"));
            }
            if(json.containsKey("exportDeclarationType") && !StringUtils.isEmpty(json.getString("exportDeclarationType"))){
                command.setExportDeclarationType(json.getString("exportDeclarationType"));
            }
            if(json.containsKey("importerSplitorderNo") && !StringUtils.isEmpty(json.getString("importerSplitorderNo"))){
                command.setImporterSplitorderNo(json.getString("importerSplitorderNo"));
            }
            if(json.containsKey("exporterSplitorderNo") && !StringUtils.isEmpty(json.getString("exporterSplitorderNo"))){
                command.setExporterSplitorderNo(json.getString("exporterSplitorderNo"));
            }
            if(json.containsKey("logisticsNo") && !StringUtils.isEmpty(json.getString("logisticsNo"))){
                command.setLogisticsNo(json.getString("logisticsNo"));
            }
            if(json.containsKey("startDate") && null != json.getDate("startDate")){
                command.setStartDate(json.getDate("startDate"));
            }
            if(json.containsKey("endDate") && null != json.getDate("endDate")){
                command.setEndDate(json.getDate("endDate"));
            }
            if(json.containsKey("originWhCode") && !StringUtils.isEmpty(json.getString("originWhCode"))){
            	command.setOriginWhCode(json.getString("originWhCode"));
            }
            if(json.containsKey("destinationWhCode") && !StringUtils.isEmpty(json.getString("destinationWhCode"))){
            	command.setDestinationWhCode(json.getString("destinationWhCode"));
            }
            command.setCustomerCode(this.getCurrentCustomerCode());
            Searchable searchable = super.buildSearchablePageParams(json);
            command.setCtx(CommandContext.getContext());
            command.setSearchable(searchable);
        } else { 
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Page<TransportationDocOrderStatusVo> page = transportationDocService.queryTranportationOrderByPage(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        if(null != page){
        	Map<String, Object> pageMap = getPageParams(page);
        	data.put("pageParams", pageMap.get("pageParams"));
        	data.put("orderList", page.content);
        }
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    public static void main(String[] args) {
    	 Map<String, Object> data = new HashMap<String, Object>();
    	 List<String> orderList = new ArrayList<String>();
    	 data.put("orderNos", orderList);
		orderList.add("T0001");
		System.out.println(JSON.toJSONString(data));
	}


    
    

   
}
